
in vec3 myNormal;
in vec3 myFragPos;
in vec2 myTextureCoords;

out vec4 color;

struct Light
{
    vec3 position;
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};
uniform Light light;

struct TextureMaterial
{
	sampler2D diffuse;
	sampler2D specular;
	float shininess;
};
uniform TextureMaterial material;
uniform vec3 viewPos;

void main()
{
	// 图片点的颜色值
	vec3 textureColor = vec3(texture(material.diffuse, myTextureCoords));
	vec3 specularColor = vec3(texture(material.specular, myTextureCoords));

	// 环境光照
	vec3 ambient = light.ambient * textureColor;

	// 漫反射光照
	vec3 norm = normalize(myNormal);
	vec3 lightDir = normalize(light.position - myFragPos);
	float diff = max(dot(norm, lightDir), 0.0f);
	vec3 diffuse = light.diffuse * diff * textureColor;

	// 镜面光照
	vec3 viewDir = normalize(viewPos - myFragPos);
	vec3 reflectDir = reflect(-lightDir, norm);
	float spec = pow(max(dot(viewDir, reflectDir), 0.0f), material.shininess);
	vec3 specular = light.specular * spec * specularColor;

	color = vec4((ambient + diffuse + specular), 1.0f);
}

